package com.ninth_group.service.Impl;


import com.ninth_group.dao.UserDao;
import com.ninth_group.dto.UserDTO;
import com.ninth_group.exception.user.UserInfoIsNullException;
import com.ninth_group.result.R;
import com.ninth_group.service.UserService;
import com.ninth_group.vo.UserVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     *  登录
     * @param userDTO 用户信息
     * @return R
     * @throws Exception 排除异常
     */
    public R login(UserDTO userDTO) throws Exception {
        // 1. 判断用户信息不为空
        if  (userDTO == null || userDTO.getUsername() == null || userDTO.getPassword() == null) {
            throw new UserInfoIsNullException("用户信息不能为空");
        }
        List<String> token = userDao.login(userDTO);
        String code = token.getFirst();
        if (code.equals("0")) return R.fail("密码错误");
        if (code.equals("1"))
            return R.ok("登录成功", new UserVO(userDTO.getUsername(), token.get(1)));
        if (code.equals("2"))
            return R.ok("注册并登录", new UserVO(userDTO.getUsername(), token.get(1)));
        return R.error("未知错误");
    }

}
